/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIPostalCity.java
 *
 * Created on 30-03-2011, 16:54:43
 */

package GUILayer;

import ControlLayer.CtrPostalCity;
import ModelLayer.PostalCity;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.IntArrayData;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
 *
 * @author Henrik-iMac
 */
public class GUIPostalCity extends javax.swing.JFrame {

    /** Creates new form GUIPostalCity */
    public GUIPostalCity() {
        initComponents();
        updateList();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tabbedPane = new javax.swing.JTabbedPane();
        searchPane = new javax.swing.JPanel();
        searchButton = new javax.swing.JButton();
        searchZipCodeField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        searchStatusLabel = new javax.swing.JLabel();
        searchClearButton = new javax.swing.JButton();
        searchCityField = new javax.swing.JTextField();
        allCitiesPane = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        insertPane = new javax.swing.JPanel();
        insertCityField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        insertZipCodeField = new javax.swing.JTextField();
        insertButton = new javax.swing.JButton();
        insertStatusLabel = new javax.swing.JLabel();
        insertClearButton = new javax.swing.JButton();
        updatePane = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        updateZipCodeField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        updateCityField = new javax.swing.JTextField();
        updateButton = new javax.swing.JButton();
        updateClearButton = new javax.swing.JButton();
        updateStatusLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 18));
        jLabel1.setText("Postal City");

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        searchZipCodeField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchZipCodeFieldActionPerformed(evt);
            }
        });

        jLabel4.setText("Zip code");

        jLabel5.setText("City");

        searchClearButton.setText("Clear");
        searchClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchClearButtonActionPerformed(evt);
            }
        });

        searchCityField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchCityFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout searchPaneLayout = new javax.swing.GroupLayout(searchPane);
        searchPane.setLayout(searchPaneLayout);
        searchPaneLayout.setHorizontalGroup(
            searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(searchPaneLayout.createSequentialGroup()
                        .addGroup(searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(41, 41, 41)
                        .addGroup(searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(searchCityField, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(searchZipCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(searchPaneLayout.createSequentialGroup()
                        .addComponent(searchButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchClearButton))
                    .addComponent(searchStatusLabel))
                .addGap(366, 366, 366))
        );
        searchPaneLayout.setVerticalGroup(
            searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(searchZipCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(searchCityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(searchPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(searchClearButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchStatusLabel)
                .addContainerGap(267, Short.MAX_VALUE))
        );

        tabbedPane.addTab("Search", searchPane);

        jLabel8.setText("Zip code");

        jLabel9.setText("City");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout allCitiesPaneLayout = new javax.swing.GroupLayout(allCitiesPane);
        allCitiesPane.setLayout(allCitiesPaneLayout);
        allCitiesPaneLayout.setHorizontalGroup(
            allCitiesPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(allCitiesPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(allCitiesPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                    .addGroup(allCitiesPaneLayout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(26, 26, 26)
                        .addComponent(jLabel9)))
                .addContainerGap())
        );
        allCitiesPaneLayout.setVerticalGroup(
            allCitiesPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(allCitiesPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(allCitiesPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(196, Short.MAX_VALUE))
        );

        tabbedPane.addTab("All cities", allCitiesPane);

        insertCityField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertCityFieldActionPerformed(evt);
            }
        });

        jLabel3.setText("City");

        jLabel2.setText("Zip code");

        insertButton.setText("Insert");
        insertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertButtonActionPerformed(evt);
            }
        });

        insertClearButton.setText("Clear");
        insertClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertClearButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout insertPaneLayout = new javax.swing.GroupLayout(insertPane);
        insertPane.setLayout(insertPaneLayout);
        insertPaneLayout.setHorizontalGroup(
            insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(insertPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(insertPaneLayout.createSequentialGroup()
                        .addGroup(insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(41, 41, 41)
                        .addGroup(insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(insertCityField, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(insertZipCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(insertPaneLayout.createSequentialGroup()
                        .addComponent(insertButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(insertClearButton))
                    .addComponent(insertStatusLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        insertPaneLayout.setVerticalGroup(
            insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(insertPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(insertZipCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(insertCityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(insertPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(insertButton)
                    .addComponent(insertClearButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(insertStatusLabel)
                .addContainerGap(267, Short.MAX_VALUE))
        );

        tabbedPane.addTab("Insert", insertPane);

        jLabel6.setText("Zip code");

        updateZipCodeField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateZipCodeFieldActionPerformed(evt);
            }
        });

        jLabel7.setText("City");

        updateButton.setText("Update");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        updateClearButton.setText("Clear");
        updateClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateClearButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout updatePaneLayout = new javax.swing.GroupLayout(updatePane);
        updatePane.setLayout(updatePaneLayout);
        updatePaneLayout.setHorizontalGroup(
            updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(updatePaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(updatePaneLayout.createSequentialGroup()
                        .addComponent(updateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(updateClearButton))
                    .addGroup(updatePaneLayout.createSequentialGroup()
                        .addGroup(updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(40, 40, 40)
                        .addGroup(updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(updateCityField, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(updateZipCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(updateStatusLabel))
                .addContainerGap(50, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        updatePaneLayout.setVerticalGroup(
            updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(updatePaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(updateZipCodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(updateCityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(updatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(updateButton)
                    .addComponent(updateClearButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(updateStatusLabel)
                .addContainerGap(267, Short.MAX_VALUE))
        );

        tabbedPane.addTab("Update", updatePane);

        jMenu1.setText("Navigation");

        jMenuItem1.setText("Main menu");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:
        search();
    }//GEN-LAST:event_searchButtonActionPerformed

    private void updateClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateClearButtonActionPerformed
        // TODO add your handling code here:
        clear();
    }//GEN-LAST:event_updateClearButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        // TODO add your handling code here:
        updateCity();
    }//GEN-LAST:event_updateButtonActionPerformed

    private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertButtonActionPerformed
        // TODO add your handling code here:
        insertCity();
        updateList();
    }//GEN-LAST:event_insertButtonActionPerformed

    private void searchZipCodeFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchZipCodeFieldActionPerformed
        // TODO add your handling code here:
        search(0);
    }//GEN-LAST:event_searchZipCodeFieldActionPerformed

    private void insertClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertClearButtonActionPerformed
        // TODO add your handling code here:
        clear();
    }//GEN-LAST:event_insertClearButtonActionPerformed

    private void searchClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchClearButtonActionPerformed
        // TODO add your handling code here:
        clear();
    }//GEN-LAST:event_searchClearButtonActionPerformed

    private void updateZipCodeFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateZipCodeFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_updateZipCodeFieldActionPerformed

    private void insertCityFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertCityFieldActionPerformed
        // TODO add your handling code here:
        insertCity();
        updateList();
    }//GEN-LAST:event_insertCityFieldActionPerformed

    private void searchCityFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchCityFieldActionPerformed
        // TODO add your handling code here:
        search(1);
    }//GEN-LAST:event_searchCityFieldActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        mainMenu();
}//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIPostalCity().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel allCitiesPane;
    private javax.swing.JButton insertButton;
    private javax.swing.JTextField insertCityField;
    private javax.swing.JButton insertClearButton;
    private javax.swing.JPanel insertPane;
    private javax.swing.JLabel insertStatusLabel;
    private javax.swing.JTextField insertZipCodeField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchCityField;
    private javax.swing.JButton searchClearButton;
    private javax.swing.JPanel searchPane;
    private javax.swing.JLabel searchStatusLabel;
    private javax.swing.JTextField searchZipCodeField;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JButton updateButton;
    private javax.swing.JTextField updateCityField;
    private javax.swing.JButton updateClearButton;
    private javax.swing.JPanel updatePane;
    private javax.swing.JLabel updateStatusLabel;
    private javax.swing.JTextField updateZipCodeField;
    // End of variables declaration//GEN-END:variables

    private void search(int i)
    {
        CtrPostalCity ctrPost = new CtrPostalCity();
        if(i == 0)
        {
            String input = searchZipCodeField.getText().trim();
            PostalCity postObj = ctrPost.findByZipCode(Integer.parseInt(input));
                if(postObj.getCity().equals("")){
                    searchStatusLabel.setText("The insert city does not exist");
                }
                else{
                    clear();
                    putValuesOnTheScreen(postObj);
                }
        }
        if(i == 1)
        {
            String input = searchCityField.getText().trim();
            PostalCity postObj = ctrPost.findByCityName(input);
            if(postObj.getCity().equals("")){
                searchStatusLabel.setText("The insert city does not exist");
            }
            else{
                clear();
                putValuesOnTheScreen(postObj);
            }
        }
    }

    private void search()
    {
        CtrPostalCity ctrPost = new CtrPostalCity();
        String input = searchZipCodeField.getText().trim();
        System.out.println(input);
        try {
            if(input.length() == 0){
                input = searchCityField.getText().trim();
                if(input.length() == 0)
                {
                    searchStatusLabel.setText("Please insert zipcode or city name");
                }
                else{
                    PostalCity postObj = ctrPost.findByCityName(input);
                    if(postObj.getCity().equals("")){
                        searchStatusLabel.setText("The insert city does not exist");
                    }
                    else{
                        clear();
                        putValuesOnTheScreen(postObj);
                    }
                }
            }
            else
            {
                PostalCity postObj = ctrPost.findByZipCode(Integer.parseInt(input));
                if(postObj.getCity().equals("")){
                    searchStatusLabel.setText("The insert city does not exist");
                }
                else{
                    clear();
                    putValuesOnTheScreen(postObj);
                }
            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }

    private void putValuesOnTheScreen(PostalCity postObj)
    {
        searchZipCodeField.setText(String.valueOf(postObj.getPostalCode()));
        searchCityField.setText(postObj.getCity());
        updateZipCodeField.setText(String.valueOf(postObj.getPostalCode()));
        updateCityField.setText(postObj.getCity());
    }

    
    private void updateCity()
    {
        CtrPostalCity ctrPostal = new CtrPostalCity();
        int zipCode = Integer.parseInt(updateZipCodeField.getText());
        System.out.println("+" + zipCode + "+");

        String city = updateCityField.getText();

        int result = ctrPostal.updatePostalCity(zipCode, city);
        if(result == 1){
            clear();
            updateStatusLabel.setText("City is updated to: " + city);
        }
        else{
            updateStatusLabel.setText("Problem with update");
        }
    }

    private void insertCity()
    {
        CtrPostalCity ctrPost = new CtrPostalCity();
        PostalCity postObj = new PostalCity();
        int zipCode = Integer.parseInt(insertZipCodeField.getText());
        String city = insertCityField.getText();
        postObj.setPostalCode(zipCode);
        postObj.setCity(city);
        ctrPost.insertPostalCity(postObj);
        clear();
        insertStatusLabel.setText(city + " is inserted");
    }

    private void mainMenu()
    {
        setVisible(false);
        GUIMainMenu gui = new GUIMainMenu();
    }

    private void supplyMenu()
    {
        setVisible(false);
        GUISupplier gui = new GUISupplier();
    }

    private void clear()
    {
        insertZipCodeField.setText(null);
        insertCityField.setText(null);
        insertStatusLabel.setText(null);
        searchZipCodeField.setText(null);
        searchCityField.setText(null);
        searchStatusLabel.setText(null);
        updateZipCodeField.setText(null);
        updateCityField.setText(null);
        updateStatusLabel.setText(null);
    }

    private void updateList()
    {
        CtrPostalCity ctrPostalCity = new CtrPostalCity();
        ArrayList<PostalCity> post = ctrPostalCity.findAllCities();
        buildList(post);
    }

    private void buildList(ArrayList<PostalCity> postalCities)
    {
        String [] cities = new String[postalCities.size()];
        int index = 0;
        for(PostalCity cityObj : postalCities)
        {
            int zip = cityObj.getPostalCode();
            cities[index] = (zip + "              " + cityObj.getCity());
            index++;
        }
        jList1.setListData(cities);
    }
}
